﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Net;
using System.IO;
using System.Data;
using CRMEntity;
using System.Web.Configuration;
using CRMBiz;

namespace CRMBiz
{
    public static class VedaService
    {
        public static Stream processCreditRequest(List<List<string>> DynamicParamter, int flagByCreditCheck)
        {
            CRMEntity.Configuration.WebServiceConfiguration webServiceConfiguration = new CRMEntity.Configuration.WebServiceConfiguration();
            if (flagByCreditCheck == 0)
                webServiceConfiguration = Configuration.WebServiceConfiguration.WebServiceSetParamter("VEDA Credit Service By Client", DynamicParamter);
            else
                webServiceConfiguration = Configuration.WebServiceConfiguration.WebServiceSetParamter("VEDA Credit Service By VedaFileNo", DynamicParamter);

            string soap = "";
            soap = webServiceConfiguration.ServiceInputXML;
            soap = soap.Replace("\"\"", "\"");

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(webServiceConfiguration.ServiceURL);
            req.ContentType = "text/xml";
            req.Method = "POST";
            using (Stream stm = req.GetRequestStream())
            {
                using (StreamWriter stmw = new StreamWriter(stm))
                {
                    stmw.Write(soap);
                }
            }

            WebResponse response = req.GetResponse();
            Stream responseStream = response.GetResponseStream();
            return responseStream;
        }
        public static byte[] ReadFully(Stream input)
        {
            byte[] buffer = new byte[16 * 1024];
            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                return ms.ToArray();
            }
        }

        public static DataSet ConvertXMLtoDataSet(string response)
        {
            DataSet ds = new DataSet();
            using (StringReader stringReader = new StringReader(response))
            {
                ds = new DataSet();
                ds.ReadXml(stringReader);
            }
            return ds;
        }

        public static VedaResponse VedaResponseXMLParser(DataSet ResponseDS)
        {
            CRMEntity.VedaResponse vedaResp = new CRMEntity.VedaResponse();
            DataTable summaryTable = ResponseDS.Tables["summary"];
            //DataTable contributingFactorTable = ResponseDS.Tables["contributing-factor"];
            vedaResp.prim_individual_cross_reference_client_reference = ResponseDS.Tables["BCAservice"].Rows[0]["BCAservice-client-ref"].ToString();
            vedaResp.prim_individual_cross_reference_bureau_reference = ResponseDS.Tables["primary-match"].Rows[0]["bureau-reference"].ToString();
            foreach (DataRow dr in summaryTable.Rows)
            {
                if (dr.ItemArray[1].ToString() == "known-identities")
                    vedaResp.known_identities = dr.ItemArray[3].ToString();
                if (dr.ItemArray[1].ToString() == "adverse-on-file")
                    vedaResp.adverse_on_file = dr.ItemArray[3].ToString();
                if (dr.ItemArray[1].ToString() == "credit-enquiries-12")
                    vedaResp.credit_enquiries_12 = dr.ItemArray[3].ToString();
                if (dr.ItemArray[1].ToString() == "credit-enquiries-60")
                    vedaResp.credit_enquiries_60 = dr.ItemArray[3].ToString();
                if (dr.ItemArray[1].ToString() == "total-value-of-outstanding-defaults")
                    vedaResp.total_value_of_outstanding_defaults = dr.ItemArray[3].ToString();
                if (dr.ItemArray[1].ToString() == "authorised-agents-enquiries-12")
                    vedaResp.authorised_agents_enquiries_12 = dr.ItemArray[3].ToString();
                if (dr.ItemArray[1].ToString() == "age-of-file")
                    vedaResp.age_of_file = dr.ItemArray[3].ToString();
                if (dr.ItemArray[1].ToString() == "directorships-current")
                    vedaResp.directorships_current = dr.ItemArray[3].ToString();
                if (dr.ItemArray[1].ToString() == "directorships-previous")
                    vedaResp.directorships_previous = dr.ItemArray[3].ToString();
                if (dr.ItemArray[1].ToString() == "proprietorships")
                    vedaResp.proprietorships = dr.ItemArray[3].ToString();
                if (dr.ItemArray[1].ToString() == "file-notes")
                    vedaResp.file_notes = dr.ItemArray[3].ToString();
                if (dr.ItemArray[1].ToString() == "authorised-agents-enquiries-60")
                    vedaResp.authorised_agents_enquiries_60 = dr.ItemArray[3].ToString();
            }
            vedaResp.veda_score = ResponseDS.Tables["score"].Rows[0]["veda-score"].ToString();
            vedaResp.relative_risk = ResponseDS.Tables["score"].Rows[0]["relative-risk"].ToString();
            vedaResp.applicant_odds = ResponseDS.Tables["score"].Rows[0]["applicant-odds"].ToString();
            vedaResp.population_odds = ResponseDS.Tables["population"].Rows[0]["population-odds"].ToString();
            vedaResp.contributing_factor_filed1 = ResponseDS.Tables["contributing-factor"].Rows[0]["impact"].ToString();
            vedaResp.contributing_factor_filed2 = ResponseDS.Tables["contributing-factor"].Rows[1]["impact"].ToString();
            vedaResp.contributing_factor_filed3 = ResponseDS.Tables["contributing-factor"].Rows[2]["impact"].ToString();
            vedaResp.contributing_factor_filed4 = ResponseDS.Tables["contributing-factor"].Rows[3]["impact"].ToString();
            vedaResp.contributing_factor_value1 = ResponseDS.Tables["contributing-factor"].Rows[0]["contributing-factor_Text"].ToString();
            vedaResp.contributing_factor_value2 = ResponseDS.Tables["contributing-factor"].Rows[1]["contributing-factor_Text"].ToString();
            vedaResp.contributing_factor_value3 = ResponseDS.Tables["contributing-factor"].Rows[2]["contributing-factor_Text"].ToString();
            vedaResp.contributing_factor_value4 = ResponseDS.Tables["contributing-factor"].Rows[3]["contributing-factor_Text"].ToString();
            return vedaResp;
        }
    }
}
